package com.shuda.business.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuda.business.crm.entity.FollowUpMethodEntity;
import com.shuda.business.crm.service.FollowUpMethodService;
import com.shuda.common.core.domain.R;
import com.shuda.common.core.domain.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 跟进方式配置Controller
 * @author shuda
 */
@RestController
@RequestMapping("/crm/follow-up-method")
@Tag(name = "跟进方式配置管理", description = "跟进方式配置相关接口")
@RequiredArgsConstructor
public class FollowUpMethodController {

    private final FollowUpMethodService followUpMethodService;

    /**
     * 分页查询跟进方式配置
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询跟进方式配置", description = "根据条件分页查询跟进方式配置")
    public R<IPage<FollowUpMethodEntity>> getFollowUpMethodPage(
            @Parameter(description = "分页请求参数") PageRequest pageRequest,
            @Parameter(description = "跟进方式名称") @RequestParam(required = false) String methodName,
            @Parameter(description = "状态") @RequestParam(required = false) String status) {
        
        Page<FollowUpMethodEntity> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        IPage<FollowUpMethodEntity> result = followUpMethodService.getFollowUpMethodPage(page, methodName, status);
        return R.ok(result);
    }

    /**
     * 查询启用的跟进方式列表
     */
    @GetMapping("/enabled")
    @Operation(summary = "查询启用的跟进方式列表", description = "查询所有启用的跟进方式配置")
    public R<List<FollowUpMethodEntity>> getEnabledFollowUpMethods() {
        List<FollowUpMethodEntity> result = followUpMethodService.getEnabledFollowUpMethods();
        return R.ok(result);
    }

    /**
     * 根据ID获取跟进方式配置
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取跟进方式配置", description = "根据ID获取跟进方式配置详情")
    public R<FollowUpMethodEntity> getFollowUpMethodById(@Parameter(description = "跟进方式ID") @PathVariable Long id) {
        FollowUpMethodEntity methodEntity = followUpMethodService.getFollowUpMethodById(id);
        return methodEntity != null ? R.ok(methodEntity) : R.fail("跟进方式配置不存在");
    }

    /**
     * 根据编码获取跟进方式配置
     */
    @GetMapping("/code/{methodCode}")
    @Operation(summary = "根据编码获取跟进方式配置", description = "根据编码获取跟进方式配置")
    public R<FollowUpMethodEntity> getFollowUpMethodByCode(
            @Parameter(description = "跟进方式编码") @PathVariable String methodCode,
            @Parameter(description = "租户ID") @RequestParam Long tenantId) {
        
        FollowUpMethodEntity methodEntity = followUpMethodService.getFollowUpMethodByCode(methodCode, tenantId);
        return methodEntity != null ? R.ok(methodEntity) : R.fail("跟进方式配置不存在");
    }

    /**
     * 创建跟进方式配置
     */
    @PostMapping
    @Operation(summary = "创建跟进方式配置", description = "创建跟进方式配置")
    public R<Boolean> createFollowUpMethod(@RequestBody FollowUpMethodEntity methodEntity) {
        boolean result = followUpMethodService.createFollowUpMethod(methodEntity);
        return result ? R.ok(true) : R.fail("创建跟进方式配置失败");
    }

    /**
     * 更新跟进方式配置
     */
    @PutMapping
    @Operation(summary = "更新跟进方式配置", description = "更新跟进方式配置")
    public R<Boolean> updateFollowUpMethod(@RequestBody FollowUpMethodEntity methodEntity) {
        boolean result = followUpMethodService.updateFollowUpMethod(methodEntity);
        return result ? R.ok(true) : R.fail("更新跟进方式配置失败");
    }

    /**
     * 删除跟进方式配置
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除跟进方式配置", description = "根据ID删除跟进方式配置")
    public R<Boolean> deleteFollowUpMethod(@Parameter(description = "跟进方式ID") @PathVariable Long id) {
        boolean result = followUpMethodService.deleteFollowUpMethod(id);
        return result ? R.ok(true) : R.fail("删除跟进方式配置失败");
    }

    /**
     * 批量删除跟进方式配置
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除跟进方式配置", description = "批量删除跟进方式配置")
    public R<Boolean> batchDeleteFollowUpMethod(@RequestBody Long[] ids) {
        boolean result = followUpMethodService.batchDeleteFollowUpMethod(ids);
        return result ? R.ok(true) : R.fail("批量删除跟进方式配置失败");
    }

    /**
     * 启用跟进方式
     */
    @PostMapping("/enable/{id}")
    @Operation(summary = "启用跟进方式", description = "启用跟进方式配置")
    public R<Boolean> enableFollowUpMethod(@Parameter(description = "跟进方式ID") @PathVariable Long id) {
        boolean result = followUpMethodService.enableFollowUpMethod(id);
        return result ? R.ok(true) : R.fail("启用跟进方式失败");
    }

    /**
     * 禁用跟进方式
     */
    @PostMapping("/disable/{id}")
    @Operation(summary = "禁用跟进方式", description = "禁用跟进方式配置")
    public R<Boolean> disableFollowUpMethod(@Parameter(description = "跟进方式ID") @PathVariable Long id) {
        boolean result = followUpMethodService.disableFollowUpMethod(id);
        return result ? R.ok(true) : R.fail("禁用跟进方式失败");
    }

    /**
     * 更新排序
     */
    @PutMapping("/sort/{id}")
    @Operation(summary = "更新排序", description = "更新跟进方式排序")
    public R<Boolean> updateSortOrder(
            @Parameter(description = "跟进方式ID") @PathVariable Long id,
            @Parameter(description = "排序号") @RequestParam Integer sortOrder) {
        
        boolean result = followUpMethodService.updateSortOrder(id, sortOrder);
        return result ? R.ok(true) : R.fail("更新排序失败");
    }
}